package com.houbank.demo.rabbitmq.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.houbank.demo.rabbitmq.person.model.PersonDO;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaoqiguo on 2016/9/21.
 */
public class JacksonUtil {
    public static ObjectMapper objectMapper;

    /**
     * 使用泛型方法，把json字符串转换为相应的JavaBean对象。
     * (1)转换为普通JavaBean：readValue(json,Student.class)
     * (2)转换为List,如List<Student>,将第二个参数传递为Student
     * [].class.然后使用Arrays.asList();方法把得到的数组转换为特定类型的List
     *
     * @param jsonStr
     * @param valueType
     * @return
     */
    public static <T> T readValue(String jsonStr, Class<T> valueType) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }

        try {
            return objectMapper.readValue(jsonStr, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * json转对象或json数组转List
     * @param jsonStr
     * @param valueTypeRef
     * @return
     */
    public static <T> T readValue(String jsonStr, TypeReference<T> valueTypeRef){
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        try {
            return objectMapper.readValue(jsonStr, valueTypeRef);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 把JavaBean转换为json字符串
     *
     * @param object
     * @return
     */
    public static String toJSon(Object object) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }

        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        PersonDO  p =  new PersonDO("1", "10086", "liubo", 20);

        List<PersonDO> list = new ArrayList<PersonDO>();
        list.add(new PersonDO("2","11","12",11));
        list.add(new PersonDO("3","13","12",11));
        list.add(new PersonDO("4","14","12",11));

        String jsonStr = JacksonUtil.toJSon(list);
        System.out.println(jsonStr);
        List<PersonDO>  ll = JacksonUtil.readValue(jsonStr, new TypeReference<List<PersonDO>>() {
        });
        System.out.println(ll.size());
    }
}
